package com.goddess.yeecolor.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import com.andview.refreshview.XRefreshView;
import com.andview.refreshview.XRefreshViewFooter;
import com.goddess.yeecolor.Modle.BaoMinInfo;
import com.goddess.yeecolor.Modle.xRefreshMethed;
import com.goddess.yeecolor.R;
import com.goddess.yeecolor.adapter.EnrollNumberAdapter;
import com.goddess.yeecolor.base.BaseActivity;
import com.goddess.yeecolor.interfaceAll.OnClickListener;
import com.goddess.yeecolor.tools.MyUrl;
import com.goddess.yeecolor.tools.OkttpUtils;
import com.goddess.yeecolor.tools.ToastUtils;
import com.goddess.yeecolor.view.RecycleViewDivider;
import com.goddess.yeecolor.view.dialog_progress;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 已报名列表
 */
public class EnrollNumberAcitivity extends BaseActivity{
    private XRefreshView xRefreshView;
    private RecyclerView mRecyclerview;
    private View mView;
    private Toolbar mToobar;
    private TextView mTitle;
    private Bundle bundle;
    private EnrollNumberAdapter adapter;
    private List<BaoMinInfo>mListBaoMing;
    private LinearLayoutManager mLinearManager;
    private int number;//报名数
    private String id;
    private String TAG="NORBOR";
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        setTheme(R.style.AppTheme);
        super.onCreate(savedInstanceState);
        mView= LayoutInflater.from(this).inflate(R.layout.activity_enrollnumbe,null);
        setContentView(mView);
        bundle=getIntent().getBundleExtra("bundle");
        number=bundle.getInt("number");
        id=bundle.getString("id");
        initFindView();
    }

    @Override
    public void initOnClck() {

    }
    private dialog_progress dialog_progress;
    private void setProgress(){
        dialog_progress=new dialog_progress(this);
        dialog_progress.requestWindowFeature(Window.FEATURE_NO_TITLE);

        dialog_progress.setCanceledOnTouchOutside(false);//按返回键可以关闭进度条
        dialog_progress.show();
    }
    @Override
    public void initFindView() {
        mToobar= (Toolbar) findViewById(R.id.title_toobar);
        mTitle= (TextView) findViewById(R.id.title_content);
        mTitle.setText("已报名");//("+number+")
        xRefreshView= (XRefreshView) findViewById(R.id.enrollnumber_xrefre);
        mRecyclerview= (RecyclerView) findViewById(R.id.enrollnumber_recyclerview);

//        setSupportActionBar(mToobar);
//        getSupportActionBar().setDisplayShowTitleEnabled(false);//隐藏副标题
        mToobar.setNavigationIcon(R.mipmap.back3x);
        mToobar.setNavigationOnClickListener((view)->{
            setFiish(this);
        });
        /**
         * reclerview的初始化
         */
        initRecycler();
    }

    private int page=1;
    private Map<Object,Object> map;
    @Override
    public void initData() {

        map=new HashMap<>();
        map.put("event_id",id);
        map.put("page",page+"");
        setRequest(MyUrl.workers,map,handler,1);
    }
    private void setRequest(String url, Map<Object,Object> map, Handler handler, int tag){
        OkttpUtils.getIntence().OkttpPost(url,map,handler,tag);
        setProgress();
    }

    /**
     * 初始化recyclerview
     */
    private int lomode;
    private void initRecycler(){
        xRefreshView= (XRefreshView) findViewById(R.id.enrollnumber_xrefre);
        mRecyclerview= (RecyclerView) findViewById(R.id.enrollnumber_recyclerview);
        mRecyclerview.setHasFixedSize(true);
        initData();

        mLinearManager = new LinearLayoutManager(this);
        mRecyclerview.setLayoutManager(mLinearManager);
        mRecyclerview.addItemDecoration(new RecycleViewDivider(this, mLinearManager.HORIZONTAL, 1, Color.rgb(211, 211, 211)));

        //设置刷新完成以后，headerview固定的时间
        xRefreshMethed.setRefrsh(xRefreshView);

        xRefreshView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener(){
            @Override
            public void onLoadMore(boolean isSilence) {
                super.onLoadMore(isSilence);
                TAG="LODE";
                page++;
                initData();

            }
            @Override
            public void onRefresh() {
                super.onRefresh();
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        xRefreshView.stopRefresh();
                    }
                },1000);
            }
        });
    }
    private JSONArray items;
    private String jsonStringj,msgs;
    private int code,count;
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            try {
                jsonStringj=msg.getData().getString("result");
                JSONObject object=new JSONObject(jsonStringj);
                code=object.getInt("code");
                msgs=object.getString("msg");
                JSONObject result=object.getJSONObject("result");
                if (code==100){
                    if (msg.what==1){//获取已报名的人数
                        mListBaoMing=new ArrayList<>();

                        count=result.getInt("count");
                        items=result.getJSONArray("items");
                        for (int i=0;i<items.length();i++){
                            JSONObject data=items.getJSONObject(i);

                            BaoMinInfo item=new BaoMinInfo();
                            item.setId(data.getInt("id"));
                            item.setUnique(data.getString("unique"));
                            item.setThumb(data.getString("thumb"));
                            item.setNickname(data.getString("nickname"));
                            item.setWeixinid(data.getString("weixinid"));
                            item.setIndustry(data.getString("industry"));
                            item.setOccupation(data.getString("occupation"));
                            item.setCompany(data.getString("company"));
                            item.setCity(data.getString("city"));
                            item.setAddtime(data.getString("addtime"));
                            if (TAG.equals("NORBOR")){
                                mListBaoMing.add(item);
                            }
                            else if (TAG.equals("LODE")){

                                adapter.insert(item,
                                        adapter.getAdapterItemCount());
                            }

                        }
                        if (TAG.equals("NORBOR")){
                            adapter = new EnrollNumberAdapter(mListBaoMing, EnrollNumberAcitivity.this);
                            mRecyclerview.setAdapter(adapter);
                            /**
                             * 写到这
                             */
                            View view=LayoutInflater.from(EnrollNumberAcitivity.this).inflate(R.layout.empty,null);
                            xRefreshView.setEmptyView(view);
                            adapter.setCustomLoadMoreView(new XRefreshViewFooter(EnrollNumberAcitivity.this));
                        }
                        else if (TAG.equals("LODE")){

                            xRefreshView.stopLoadMore(true);

                        }
                    }
                }
                else if (code==403){
                    String msgs=result.getString("msg");
                    ToastUtils.toast(msgs);
                    if (msgs.equals("暂无数据！")){
                       if (TAG.equals("LODE")){

                            xRefreshView.stopLoadMore(true);
                           xRefreshView.setLoadComplete(true);

                       }
                    }

                }


                // 静默加载模式不能设置footerview

                adapter.setOnClickListener(new OnClickListener() {
                    @Override
                    public void OnClick(View view, int position) {

                    }

                    @Override
                    public void OnLongClick(View view, int position) {

                    }
                });
                if (dialog_progress.isShowing()){
                    dialog_progress.dismiss();
                }
            }
            catch (Exception e){
                e.printStackTrace();
            }
        }
    };
}
